﻿using MyNamespace;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Zhaoxi.CSharpDemo.CSharp10;
using Zhaoxi.CSharpDemo.CSharp8;
using Zhaoxi.CSharpDemo.CSharp9;

namespace Zhaoxi.CSharpDemo
{
    public class CSharp10Info
    {
        public static void Show()
        {
            #region 结构记录类型
            {
                PersonStruct person = new PersonStruct()
                {
                    Age = 36,
                    Gender = Gender.Male,
                    Name = "Richard老师"
                };

                //person.Age = 40; //不允许 
                PersonNewStruct personNewStruct = new PersonNewStruct()
                {
                    Age = 36,
                    Name = "Richard老师",
                    Gender = Gender.Male,
                };

                //personNewStruct.Age = 40; 
                personNewStruct.Deconstruct(out string name, out int age, out Gender gender);
                Console.WriteLine($"personNewStruct.name={name}");
                Console.WriteLine($"personNewStruct.age={age}");
                Console.WriteLine($"personNewStruct.gender={gender}");

                //with表达式--得到一个结构的副本
                {
                    PersonNewStruct personNew = personNewStruct with { Name = "Sunnie" };
                    Console.WriteLine($"personNew.Name={personNew.Name}");
                    Console.WriteLine($"personNewStruct.gender={gender}");
                }
            }
            #endregion

            #region 无参数结构构造函数
            {
                PersonInfo01 person = new PersonInfo01();
                Console.WriteLine($"person.Id={person.Id}");
                Console.WriteLine($"person.Name={person.Name}");

                PersonInfo02 person2 = new PersonInfo02("Richard老师");
                Console.WriteLine($"person2.Id={person2.Id}");
                Console.WriteLine($"person2.Name={person2.Name}");

                PersonInfo02 personNoCtor = new PersonInfo02();
                Console.WriteLine($"personNoCtor.Id={personNoCtor.Id}");
                Console.WriteLine($"personNoCtor.Name={personNoCtor.Name}");

                PersonInfo03 personInfo03 = new PersonInfo03();
                Console.WriteLine($"personInfo03.Id={personInfo03.Id}");
                Console.WriteLine($"personInfo03.Name={personInfo03.Name}");

                PersonInfo03 personInfoNew = new PersonInfo03("Richard老师");
                Console.WriteLine($"personInfoNew.Id={personInfoNew.Id}");
                Console.WriteLine($"personInfoNew.Name={personInfoNew.Name}");
            }
            #endregion

            #region 全局使用using指令
            {
                //Type type = typeof(PersonInfo01);
                //Activator.CreateInstance(type);
                //Assembly assembly = Assembly.LoadFrom("Zhaoxi.CSharpDemo.dll");

                //隐式usings
                //隐式usings功能会自动在构建的项目类型添加通用的全局using指令。 
                //要启用隐式usings，请在.csproj 文件中设置 ImplicitUsings 属性：


                //静态全局using
            }
            #endregion

            #region 文件范围命名空间 
            //去掉大括号  在当前文件下的class 都归属于当前命名空间
            {
                Class1 class1 = new Class1();
                Class2 class2 = new Class2();
                Class3 class3 = new Class3();
                Test test = new Test();
            }
            #endregion

            #region 扩展属性模式
            {
                object obj = new UserInfo
                {
                    UserId = 123,
                    UserName = "Richard老师",
                    Age = 36,
                    Address = new CSharp10.Address()
                    {
                        City = "WuHan"
                    }
                };



                if (obj is UserInfo { Address: { City: "WuHan" } })
                {
                    Console.WriteLine("WuHan");
                }


                if (obj is UserInfo { Address.City: "WuHan" }) // Extended property pattern
                {
                    Console.WriteLine("WuHan");
                }
            }
            #endregion

            #region Lambda 改进
            {
                //Lambda标记特性
                {
                    var f1 = [TestAttribute] () => { };               // [A] lambda 
                    var method = f1.GetMethodInfo();
                    TestAttribute attribute = method.GetCustomAttribute<TestAttribute>();
                    attribute.Show();
                }
                //Lambda标记特性 静态修饰
                {
                    var f2 = [Test] static (int x) => x;     // syntax error at '=>' 
                    var method = f2.GetMethodInfo();
                    TestAttribute attribute = method.GetCustomAttribute<TestAttribute>();
                    attribute.Show();
                }

                //Lambda标记特性修饰返回值
                {
                    var f3 = [return: TestAttribute] (int x) => x;    // syntax error at '=>'
                    ICustomAttributeProvider provider = f3.GetMethodInfo().ReturnTypeCustomAttributes;
                    object[] attributeArray = provider.GetCustomAttributes(typeof(TestAttribute), true);
                    foreach (TestAttribute attribute in attributeArray)
                    {
                        attribute.Show();
                    }
                }

                //Lambda标记特性修饰参数
                {
                    var f4 = ([TestAttribute] int x) => x;            // [A] x 
                    ParameterInfo[] prarArray = f4.GetMethodInfo().GetParameters();
                    foreach (var para in prarArray)
                    {
                        TestAttribute attribute = para.GetCustomAttribute<TestAttribute>();
                        attribute.Show();
                    }
                }
                //Lambda标记特性修饰参数
                {
                    var f5 = ([TestAttribute] ref int x) => x;        // [A] x
                    ParameterInfo[] prarArray = f5.GetMethodInfo().GetParameters();
                    foreach (var para in prarArray)
                    {
                        TestAttribute attribute = para.GetCustomAttribute<TestAttribute>();
                        attribute.Show();
                    }
                }
          }
            #endregion


            #region 常数内插字符串
            {
                const string Language = "C#";
                const string Platform = ".NET";
                const string Version = "10.0";
                const string FullProductName = $"{Platform} - Language: {Language} Version: {Version}";

                Console.WriteLine($"Language:{Language}");
                Console.WriteLine($"Platform:{Platform}");
                Console.WriteLine($"Version:{Version}");
                Console.WriteLine($"FullProductName:{FullProductName}");
            }
            #endregion


            #region MyRegion
            {
               
            }
            #endregion

        }
    }


    public class TestAttribute : Attribute
    {
        public void Show()
        {
            Console.WriteLine($"{this.GetType().FullName} 被构造~~");
        }
    }
}


